package umich.max.geolocation.experiments.stacking;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import max.nlp.util.basic.TimeUtils;
import max.nlp.wrappers.ml.weka.NClassClassifier;
import max.nlp.wrappers.ml.weka.WekaFeatureBuilder;
import umich.max.geolocation.experiments.BlogExperiment;
import umich.max.geolocation.util.BlogUtils;

public class Trainer extends BlogExperiment  {

	private String featureBuilder;
	private List<String> extractors;
	private List<String> classes;

	public Trainer(String fb, List<String> extractors, List<String> classes) {
		super();
		this.featureBuilder = fb;
		this.extractors = extractors;
		this.classes = classes;
	}

	public void run() {

		long start = System.currentTimeMillis();

		// Setup experiment directory
		String dirName = classes.get(0);

		String fullDir = "/home/max/workspace/geolocation/experiments/outlier/"
				+ dirName + "/";
		setupDirectory(fullDir);
		System.out.println(fullDir);
		// get feature extractors
		WekaFeatureBuilder bfb = getFeatureBuilder(featureBuilder, extractors);

		// Make and configure classifier
		NClassClassifier c = new NClassClassifier(classes, bfb);

		c.setExperimentDir(fullDir);
		c.shouldUseSaved(false);
		c.shouldSaveResults(true);
		c.shouldBalanceClasses(true);
		c.evaluateWithCrossFoldValidation();

		c.run();

		// Finish
		long end = System.currentTimeMillis();
		long duration = (end - start);
		System.out.println("Duration : "
				+ TimeUtils.getDurationBreakdown(duration));

	}

	public static void main(String[] args) {

		Set<String> allStates = BlogUtils.getAllStates();
		for (String s : allStates) {
			System.out.println("Training classifier for" + s);
			String fb = "blog2";

			final String state = s;
			List<String> states = new ArrayList<String>() {
				{
					add(state);
					add("ANY");
				}
			};

			List<String> extractors = new ArrayList<String>() {
				{
					add("tf");
					add("bLoc");
					add("mpLoc");
					add("perLoc");
				}
			};
			Trainer tc = new Trainer(fb, extractors, states);
			tc.run();
		}
	}

}
